// 自定义异常过滤器
import {ExceptionFilter, ArgumentsHost, BadRequestException, Catch, Inject} from "@nestjs/common";

// 内置的异常类，想要自定义他的相应内容，使用Catch捕获该类
@Catch(BadRequestException)
export class CustomExceptionFilters implements ExceptionFilter {
    // 2.2如果拦截器有参数，useFilter如何传递
    constructor(@Inject('PREFIX') private prefix:string) {
    }

    catch(exception: any, host: ArgumentsHost) {
        console.log(this.prefix)
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const requese = ctx.getRequest();
        const next = ctx.getNext();

        const status = exception.getStatus();
        response.status(status).json({
            statusCode: status,
            message: exception.getResponse(),
            timestamp: new Date().toDateString(),
            path: requese.url,
            prefix:this.prefix
        })
    }
}
